package com.sdk.pay.transfer.utils;


import com.sdk.pay.transfer.BuildConfig;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import static com.sdk.pay.transfer.constant.WechatConstants.WECHAT_ACTIVITY_PERSON_CENTER;
import static com.sdk.pay.transfer.constant.WechatConstants.WECHAT_COLLECT_MAINUI;
import static com.sdk.pay.transfer.constant.WechatConstants.WECHAT_PACKAGE_NAME;

public class ActivityHelper {
    private static final String MAIN_PRODUCT_ACTIVITY = "com.sdk.pay.transfer.MainActivity";
    public static String MY_PACKAGE = BuildConfig.APPLICATION_ID;

    private static OutputStream os;

    public static void exec(String cmd) {
        try {
            if (os == null) {
                os = Runtime.getRuntime().exec("su").getOutputStream();
            }
            os.write(cmd.getBytes());
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUitl.getInstance().saveExceptionFile("ActivityHelper exec Exception = " + e.getMessage());
        }
    }

    public static void click(String x, String y) {
        exec(String.format("input tap %s %s \n", x, y));
    }

    public static String getTopActivity() {
        String cmd = "dumpsys activity | grep mFocusedActivity";
        String nowactivity = execRootCmd(cmd);
//        MainActivity.instance.log("now activity:" + nowactivity);
        return nowactivity;
    }

    public static Boolean topActivityEquals(String cName) {
        String cmd = "dumpsys activity | grep mFocusedActivity";
        String nowactivity = execRootCmd(cmd);
//        MainActivity.instance.log("now activity:" + nowactivity);
        return nowactivity.contains(cName);
    }

    public static String execRootCmd(String cmd) {
        String result = "";
        DataOutputStream dos = null;
        DataInputStream dis = null;

        try {
            Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
            dos = new DataOutputStream(p.getOutputStream());
            dis = new DataInputStream(p.getInputStream());

//            MainActivity.instance.log(cmd);
            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            String line = null;
            while ((line = dis.readLine()) != null) {
//                MainActivity.instance.log("result = " + line);
                result += line;
            }
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUitl.getInstance().saveExceptionFile("ActivityHelper execRootCmd Exception = "
                    + e.getMessage());
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    ExceptionUitl.getInstance().saveExceptionFile("ActivityHelper execRootCmd " +
                            "IOException = " + e.getMessage());
                }
            }
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    ExceptionUitl.getInstance().saveExceptionFile("ActivityHelper execRootCmd " +
                            "IOException = " + e.getMessage());
                }
            }
        }
        return result;
    }

    public static void startActivity(String packageName, String activityName) {
        String cmd = String.format("am start -n %s/%s", packageName,
                activityName);
        execRootCmd(cmd);
    }

    public static void startService(String packageName, String serviceName) {
        String cmd = String.format("am startservice -n %s/%s", packageName,
                serviceName);
        execRootCmd(cmd);
    }

    public static void startProductQRActivity() {
        startActivity(MY_PACKAGE, MAIN_PRODUCT_ACTIVITY);
    }

    public static void startQRActivity() {
        startActivity(WECHAT_PACKAGE_NAME, WECHAT_COLLECT_MAINUI);
    }

    public static void startPersonCenterActivity() {
        startActivity(WECHAT_PACKAGE_NAME, WECHAT_ACTIVITY_PERSON_CENTER);
    }
}
